/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.notaria.guerrasalas.bc;

import gob.satt.comun.jpa.Command;
import java.util.Calendar;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import oracle.toplink.essentials.config.HintValues;
import oracle.toplink.essentials.config.TopLinkQueryHints;

/**
 *
 * @author Administrador
 */
public class GenerarNumeroTicketCommand implements Command<Integer>{

    private Integer anio;

    public GenerarNumeroTicketCommand() {
        anio = Calendar.getInstance().get(Calendar.YEAR);
    }

    @Override
    public Integer execute(EntityManager em) throws Exception {
        Integer numeroTicket = 0;
        try {
        numeroTicket = (Integer)(em.createQuery(
                "SELECT MAX(t.numero) FROM Ticket t WHERE t.anio = :anio")
                .setParameter("anio", anio)
                .setHint(TopLinkQueryHints.REFRESH, HintValues.TRUE).getSingleResult());
        } catch (NoResultException e) {
            numeroTicket = 0;
        }
        return (numeroTicket+1);
    }


}
